<template>
	<view class="wx-pay">
		<headTop>
			<uv-navbar title="帮助中心" @leftClick="leftClick" bgColor="rgba(255,255,255,.2)"></uv-navbar>
		</headTop>
		<view class="content">
			<view class="title">
				<img src="@/static/svg/weixin.svg" class="icon" />
				<span style="font-size: 16px">微信支付</span>
			</view>
			<view style="padding: 0 0 10px 0">
				<span style="font-size: 16px">支付金额:</span>
				<span style="font-size: 20px; font-weight: bold; color: #f56c6c">¥{{ payData.amount }}</span>
			</view>
			<view
				style="background-color: #f5f5f5; padding: 20px; margin-left: 50%; transform: translate(-50%); width: 200px; height: 200px">
				<uv-qrcode ref=" qrcode" size="200px" :value="payData.payUrl"></uv-qrcode>
			</view>
			<view style="font-size: 16px">
				支付单号:
				<span style="color: #999">{{ payData.out_trade_no }}</span>
			</view>
			<view style="font-size: 16px; padding: 5px 0">
				手续费:
				<span style="color: #f56c6c">¥{{ payData.fee }}</span>
			</view>
			<view class="pay-guide">
				<view class="guide-steps">
					<view class="step">
						<view class="step-number">1</view>
						<view class="step-content">打开手机微信</view>
					</view>
					<view class="step">
						<view class="step-number">2</view>
						<view class="step-content">点击"扫一扫"</view>
					</view>
					<view class="step">
						<view class="step-number">3</view>
						<view class="step-content">扫描二维码支付</view>
					</view>
				</view>
			</view>
			<!-- 支付状态 -->
			<view class="pay-footer">
				<button :type="payInfo ? 'primary' : 'info'" class="pay-button"
					round>{{ payInfo ? "已完成支付..." : "支付中..." }}</button>
			</view>
		</view>
	</view>
</template>

<script lang="ts" setup>
	import { ref, onBeforeUnmount } from "vue";
	import { userApi, tradeApi } from "@/api";
	import { onLoad } from "@dcloudio/uni-app";
	import headTop from "@/components/headTop/index.vue";

	const leftClick = () => {
		uni.navigateBack({
			delta: 1
		});
	};

	const payInfo = ref(false);
	const payParams = ref({
		type: "weixin",
		amount: 0.1
	});

	const payData = ref({
		amount: 1,
		fee: 0,
		out_trade_no: "",
		payUrl: "",
		type: ""
	});

	onLoad((options) => {
		tradeApi
			.recharge({ amount: Number(options.amount), type: options.type })
			.then((res) => {
				payData.value = res.data;
			})
			.catch((err) => {
				console.log(err);
			});
	});

	//定时器
	const timer = ref<any>(0);

	timer.value = setInterval(() => {
		tradeApi.querypaytrade({ out_trade_no: payData.value.out_trade_no }).then(() => {
			leftClick();
			clearTiemr();
			payInfo.value = true;
			uni.showToast({
				icon: "success",
				title: "充值成功"
			});
		});
	}, 5000);

	//手动销毁
	const clearTiemr = () => {
		clearInterval(timer.value);
	};

	//关闭页面销毁
	onBeforeUnmount(() => {
		clearInterval(timer.value);
	});
</script>

<style lang="scss">
	.wx-pay {
		max-height: 100vh;

		.content {
			width: 360px;
			padding: 20px 0;
			margin: 5vh auto 0 auto;
			text-align: center;
			background-color: #fff;

			.title {
				display: flex;
				justify-content: center;
				align-items: center;
				/* 垂直居中 */
				gap: 10px;
				/* 可选：设置图片和文字的间距 */

				.icon {
					width: 35px;
					height: 35px;
				}
			}

			.pay-guide {
				margin-top: 20px;

				.guide-steps {
					display: flex;
					justify-content: space-between;
					padding: 0 20px;

					.step {
						display: flex;
						flex-direction: column;
						align-items: center;
						width: 30%;

						.step-number {
							width: 28px;
							height: 28px;
							line-height: 28px;
							text-align: center;
							background-color: #07c160;
							color: #fff;
							border-radius: 50%;
							margin-bottom: 8px;
							font-size: 14px;
						}

						.step-content {
							font-size: 12px;
							color: #666;
							text-align: center;
						}
					}
				}
			}

			.pay-footer {
				display: flex;
				flex-direction: column;
				align-items: center;
				margin-top: 30px;
			}

			.pay-button {
				width: 80%;
				height: 48px;
				margin-bottom: 15px;
			}

			.cancel-button {
				width: 80%;
				height: 48px;
			}
		}
	}
</style>